package com.zgc.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zgc.entity.RolePermission;
import com.zgc.mapper.RolePermissionMapper;
import com.zgc.service.RolePermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {
    @Override
    public void saveAll(String roleid, List perIds) {
        if(StringUtils.isEmpty(roleid)||null==perIds ||perIds.size()==0){
            return ;
        }
//1.先删除之之前添加过的
        LambdaQueryWrapper<RolePermission> rolePermissionLambdaQueryWrapper = new
                LambdaQueryWrapper<>();
        rolePermissionLambdaQueryWrapper.eq(RolePermission::getRoleId,roleid);
        int count2 = this.count(rolePermissionLambdaQueryWrapper);
        if(count2 > 0){
            baseMapper.delete(rolePermissionLambdaQueryWrapper);
        }
//2.菜单是数组，拆分完成之后再保存
        for (int i = 0; i <perIds.size() ; i++) {
// 如果是根节点（1，2，3），不用保存到中间表中
            RolePermission rolePermission=new RolePermission();
            rolePermission.setRoleId(Long.parseLong(roleid));
            rolePermission.setPermissionId(Long.parseLong((String)
                    perIds.get(i)));
            baseMapper.insert(rolePermission);
        }
    }
}